﻿using AgencyManagerSystem.Common;
using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Dapper;
using Dapper.Contrib.Extensions;
using System.Data.SqlClient;

namespace AgencyManagerSystem.DataFetcher
{
    public static class ERPHelper
    {
        //static IDbConnection connection;
        private static IDbConnection CreateDbConnection()
        {
            IDbConnection connection = null;
            if (connection == null)
            {
                LogHelper.Log(typeof(ERPHelper).Name,String.Format("GETDBCONNECTION:尝试取得数据库连接:{2}@{0}:{1}", Variable.ERP_HOST, Variable.ERP_PORT,Variable.ERP_DBNAME));
                connection = new SqlConnection();
                connection.ConnectionString = String.Format("Server={0},{4};Database={1};Uid={2};Pwd={3}",
                    Variable.ERP_HOST, Variable.ERP_DBNAME, Variable.ERP_USER, Variable.ERP_PASSWORD, Variable.ERP_PORT);
            }
            return connection;
        }
        
        public static IEnumerable<T> Query<T>(string sql, DynamicParameters param = null)
        {
            return CreateDbConnection().Query<T>(sql, param);
        }
        public static T ExecuteScalar<T>( string sql, DynamicParameters param = null)
        {
            return CreateDbConnection().ExecuteScalar<T>(sql, param);
        }
        public static int Execute(string sql, DynamicParameters param = null)
        {
            return CreateDbConnection().Execute(sql, param);
        }
    }
}
